package base;
import java.util.*;
import java.io.*;

/**
 * @author tuxiaobei
 * @version 1.00
 */
public class Gourmet_Coffee_System {

    /**
     * Default constructor
     */
    public Gourmet_Coffee_System() {
    }


    /**
     * 
     */
    Sale sale = new Sale();
    Catalog catalog = null;
    Order order = new Order();

    /**
     * Lists the code and description of each product.
     */
    private void displayCatalog() {
        Iterator<Product> it = catalog.getItemsIterator();
        while(it.hasNext()) {
            System.out.print(it.next().print());
        }
    }

    /**
     * Display a product
     * @param code product code
     */
    private void displayProduct(String code) {
        System.out.print(catalog.getProduct(code));
    }

    /**
     * Display the current order:
     * Lists quantity, code, and price of each product in the
     * current order, and the total of the order.
     */
    private void displayOrder() {
        System.out.print(order);
    }

    /**
     * Add a product to the current order:
     * If the specified product is already part of the
     * order, this command will modify the quantity of that product.
     * @param code product code
     */
    private void addProductToOrder(String code) {
        Product p = catalog.getProduct(code);
        if (p == null) {
            System.out.println("The product doesn't exist!");
            return;
        }
        OrderItem q = order.getOrderItem(p);
        if (q != null) {
            q.addQuantity();
            return;
        }
        q = new OrderItem(p, 1);
        order.addOrderItem(q);
    }

    /**
     * Remove a product from the current order
     * @param code product code
     */
    private void removeProductFromOrder(String code) {
        Product p = catalog.getProduct(code);
        if (p == null) {
            System.out.println("The product doesn't exist!");
            return;
        }
        OrderItem q = order.getOrderItem(p);
        if (q == null) {
            System.out.println("The product doesn't in the current order!");
            return;
        }
        q.subQuantity();
        if (q.getQuantity() == 0) order.removeOrderItem(q);
    }

    /**
     * Register the sale of the current order: This command adds the order to the store's
     * sales and empties the current order.
     */
    private void registerSale() {
        sale.addOrder(order);
        order = new Order();
    }

    /**
     *Display the sales: Lists all the orders that have been sold.
     */
    private void displaySales() {
        Iterator<Order> it = sale.getItemsIterator();
        int num = sale.getNumberOfOrders();
        if (num == 0) {
            System.out.println("No order yet.");
        } else if (num == 1){
            System.out.println("There is 1 order.");
        } else {
            System.out.println("There are " + num + " orders.");
        }
        float sum = 0;
        while(it.hasNext()) {
            Order p = it.next();
            System.out.print(p);
            sum += p.getValue();
        }
        System.out.println("Total sales:" + String.format("%.2f", sum));
    }

    /**
     * Display number of orders with a specific product
     * @param code product code
     */
    private void displayNumOrderOfProduct(String code) {
        Product p = catalog.getProduct(code);
        if (p == null) {
            System.out.println("The product doesn't exist!");
            return;
        }
        Iterator<Order> it = sale.getItemsIterator();
        int ans = 0;
        while(it.hasNext()) {
            OrderItem q = it.next().getOrderItem(p);
            if (q != null) {
                ans += 1;
            }
        }
        System.out.println("There are " + ans + " orders include this product");
    }

    /**
     * Display the total quantity sold for each product choice
     */
    private void displayQuantityOfProduct(String code) {
        Product p = catalog.getProduct(code);
        if (p == null) {
            System.out.println("The product doesn't exist!");
            return;
        }
        Iterator<Order> it = sale.getItemsIterator();
        int ans = 0;
        while(it.hasNext()) {
            OrderItem q = it.next().getOrderItem(p);
            if (q != null) {
                ans += q.getQuantity();
            }
        }
        System.out.println("The total quantity sold of the product is " + ans);
    }

    /**
     *menu
     */

    private void system_menu() {
        Scanner reader = new Scanner(System.in);
        System.out.println("[0] Quit");
        System.out.println("[1] Display catalog");
        System.out.println("[2] Display product");
        System.out.println("[3] Display current order");
        System.out.println("[4] Add product to in current order");
        System.out.println("[5] Remove product from current order");
        System.out.println("[6] Register sale of current order");
        System.out.println("[7] Display sales");
        System.out.println("[8] Display number of orders with a specific product");
        System.out.println("[9] Display the total quantity sold for each product choice");
        System.out.print("Choice> ");
        String code = null;
        try {
            int op = reader.nextInt();
            switch (op) {
                case 0:
                    System.out.println("Goodbye!");
                    System.exit(0);
                    break;
                case 1:
                    displayCatalog();
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                case 2:
                    System.out.print("Please input product code> ");
                    code = reader.next();
                    displayProduct(code);
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                case 3:
                    displayOrder();
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                case 4:
                    System.out.print("Please input product code> ");
                    code = reader.next();
                    addProductToOrder(code);
                    break;
                case 5:
                    System.out.print("Please input product code> ");
                    code = reader.next();
                    removeProductFromOrder(code);
                    break;
                case 6:
                    registerSale();
                    break;
                case 7:
                    displaySales();
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                case 8:
                    System.out.print("Please input product code> ");
                    code = reader.next();
                    displayNumOrderOfProduct(code);
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                case 9:
                    System.out.print("Please input product code> ");
                    code = reader.next();
                    displayQuantityOfProduct(code);
                    System.out.println("Press enter to continue");
                    reader.nextLine();
                    reader.nextLine();
                    break;
                default:
                    System.out.println("Input error!");
                    break;
            }
        } catch (Exception e) {
            System.out.println("Input error!");
        }
    }

    /**
     * run
     */
    public void start() {
        catalog = new Catalog("data/catalog.csv");
        while (true) {
            system_menu();
        }

    }

}